package com.tianqu.tms.www.proxymember.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.ProxyCarInfo;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.proxymember.actionform.ProxymemberCreationInputActionForm;
import com.tianqu.tms.www.proxymember.viewhelper.ProxyMemberCarSearchInputViewHelper;


/**
 * @author zhangxueliang
 * 待办登记 - 信息录入controller
 */
public class ProxyMemberCarCreationInputController extends TmsController{

	private Logger logger = Logger
	.getLogger(ProxyMemberCarCreationInputController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		try{
			controller.begin();
			ProxymemberCreationInputActionForm actionFrom = (ProxymemberCreationInputActionForm) uc.getInputForm();
			String oldVin = (String)uc.getAttribute("vin");
			String vin = actionFrom.getVin();
			if(!"".equals(oldVin) && oldVin != null){
				vin = oldVin;
			}
			
			uc.setAttribute("vin", vin);
			ProxyCarInfo proxyCarInfo = ProxyCarInfo.getInstance();
			ProxyCarInfo proxy = ProxyCarInfo.getInstance();			
			ProxyMemberCarSearchInputViewHelper viewHelper = new ProxyMemberCarSearchInputViewHelper();
			if(!"".equals(vin)){
				proxy = proxyCarInfo.findFromMarketByVin(vin);
				if("".equals(proxy.getSerial())){
					proxy = proxyCarInfo.findFromBaseByVin(vin);//如果在marketCarInfo中没查到
					if(!"".equals(proxy.getSerial())){
						toViewHelper(proxy,viewHelper);//在base表中查询
					}
				}else{
					toViewHelper(proxy,viewHelper);
				}
			}
			Codes code = Codes.getInstance();
			viewHelper.setCarTypeList(code
					.getCodeslistByItemId(Codes.VEHICLE_TYPE));// 车辆类型
			viewHelper.setColorList(code
					.getCodeslistByItemId(Codes.VEHICLE_COLOR));// 车辆颜色
			viewHelper.setCarUseList(code
					.getCodeslistByItemId(Codes.USER_NATURE));// 使用性质list
			viewHelper.setCardTypeList(code
					.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
			viewHelper.setFuelTypeList(code
					.getCodeslistByItemId(Codes.FUEL_TYPE));// 燃料种类list
			viewHelper.setCountryStyleList(code
					.getCodeslistByItemId(Codes.VEHICLE_COUNTRY));// 国别list
			viewHelper.setAttributeList(code
					.getCodeslistByItemId(Codes.ATTACHMENT_PROPERTY));// 车辆状态list'
			viewHelper.setOwnerTypeList(code
					.getCodeslistByItemId(Codes.CAR_OWNER));// 车主类型list
			
			// 车辆品牌
			Vendor vendor = Vendor.getInstance();
			viewHelper
					.setVendorList(vendor.findAllVendorInfo());
			viewHelper.setVin(vin);
			
			viewHelper.setGearBoxList(code.getCodeslistByItemId(Codes.GEARBOX_TYPE));//变速箱类型list
			uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);
			
			
			controller.commit();
		}catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
	
	/**
	 * entity -> viewhelper
	 * @param entity
	 * @return
	 * @throws TmsDaoException 
	 */
	private void toViewHelper(ProxyCarInfo entity,ProxyMemberCarSearchInputViewHelper viewHelper) throws TmsDaoException{
		
		viewHelper.setBrandId(entity.getBrandId());
		viewHelper.setVendorId(entity.getVendorId());
		viewHelper.setCarStyleId(entity.getCarStyleId());
		viewHelper.setCardType(Integer.toString(entity.getCardType()));
		viewHelper.setLicensePlate(entity.getLicensePlate());
		viewHelper.setCarOwner(entity.getCarOwner());
		viewHelper.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		viewHelper.setCarType(Integer.toString(entity.getCarType()));
		viewHelper.setCarUse(Integer.toString(entity.getCarUse()));
		viewHelper.setColor(Integer.toString(entity.getColor()));
		viewHelper.setCompanyContactPerson(entity.getCompanyContactPerson());
		viewHelper.setDisplacement(String.valueOf(entity.getDisplacement()));
		viewHelper.setCountryStyle(Integer.toString(entity.getCountryStyle()));
		
		viewHelper.setEngine(entity.getEngine());
		viewHelper.setFuelType(Integer.toString(entity.getFuelType()));
		viewHelper.setGearbox(Integer.toString(entity.getGearbox()));
		viewHelper.setJqx(entity.getJqx());
		if (entity.getLoadquality() == 0) {
			viewHelper.setLoadquality("");
		} else {
			viewHelper.setLoadquality(Integer.toString(entity.getLoadquality()));
		}
	
		if (entity.getMileage() == 0) {
			viewHelper.setMileage("");
		} else {
			viewHelper.setMileage(Integer.toString(entity.getMileage()));
		}

		viewHelper.setModelCode(entity.getModelCode());
		viewHelper.setOwnerAddress(entity.getOwnerAddress());
		viewHelper.setOwnerOrgNo(entity.getOwnerOrgNo());
		viewHelper.setOwnerPhone(entity.getOwnerPhone());
		viewHelper.setOwnerType(Integer.toString(entity.getOwnerType()));
		if (entity.getPassenger() == 0) {
			viewHelper.setPassenger("");
		} else {
			viewHelper.setPassenger(Integer.toString(entity.getPassenger()));
		}
		if (entity.getPrice() == 0) {
			viewHelper.setPrice("");
		} else {
			viewHelper.setPrice(Integer.toString(entity.getPrice()));
		}

		viewHelper.setRegDate(entity.getRegDate());
		viewHelper.setRegisterCardNo(entity.getRegisterCardNo());
		viewHelper.setSendCardDate(entity.getSendCardDate());
		viewHelper.setSerial(entity.getSerial());
		viewHelper.setUpdateTime(entity.getUpdateTime());
		viewHelper.setUpdateUserId(entity.getUpdateUserId());
		viewHelper.setVendorCode(entity.getVendorCode());
		viewHelper.setYearCarefulPeriod(entity.getYearCarefulPeriod());
		
	}

}
